package com.aaa.stock.service.Impl;

import com.aaa.entity.Medicines;
import com.aaa.stock.dao.MedicinalDao;
import com.aaa.stock.service.MedicinesService;
import com.aaa.stock.vo.MedicinesVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

@Service
public class MedicinesServiceImpl extends ServiceImpl<MedicinalDao, Medicines> implements MedicinesService {
    @Autowired
    private MedicinalDao medicinalDao;

    @Override
    public Result<IPage<Medicines>> findByConditio(Integer currentPage, Integer pageSize, MedicinesVo medicinesVo) {
        IPage<Medicines> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Medicines> wrapper = new QueryWrapper<>();
        //药品名称
        if (StringUtils.hasText(medicinesVo.getMedicinesName())) {
            wrapper.like("medicines_name", medicinesVo.getMedicinesName());
        }
        //关键字
        if (StringUtils.hasText(medicinesVo.getKeywords())) {
            wrapper.like("keywords", medicinesVo.getKeywords());
        }
        //药品类型
        if (StringUtils.hasText(medicinesVo.getMedicinesType())) {
            wrapper.eq("medicines_type", medicinesVo.getMedicinesType());
        }
        //生产厂家
        if (StringUtils.hasText(medicinesVo.getProducterId())) {
            wrapper.eq("producter_id", medicinesVo.getProducterId());
        }
        //处方类型
            if (StringUtils.hasText(medicinesVo.getPrescriptionType())) {
            wrapper.eq("prescription_type", medicinesVo.getPrescriptionType());
        }

        //状态
        if (StringUtils.hasText(medicinesVo.getStatus())) {
            wrapper.eq("status", medicinesVo.getStatus());
        }

        IPage<Medicines> page1 = medicinalDao.selectPage(page, wrapper);
        return new Result<IPage<Medicines>>(200, "查询用户成功", page1);

    }

    /*
    * name:xiaoxuan;
    * description:获取一个map来修改库存，map里面包含<主键，要减去的库存>
    */
    @Override
    public void downInve(Map<String, BigDecimal> map) {
        System.out.println("22222222222222222222");
        System.out.println(map);
        for (String s : map.keySet()) {
            System.out.println("22222222======"+s);
            QueryWrapper<Medicines> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("medicines_id",s);
            Medicines medicines = medicinalDao.selectOne(queryWrapper);

            System.out.println("3333333333333333");
            System.out.println(medicines);
            System.out.println("3333333333333333");

            BigDecimal medicinesStockNum = medicines.getMedicinesStockNum();
            BigDecimal bigDecimal = map.get(s);

            System.out.println("5555555555555555555");
            System.out.println(medicinesStockNum.subtract(bigDecimal));
            System.out.println("5555555555555555555");

            UpdateWrapper<Medicines> wrapper = new UpdateWrapper<>();
            wrapper.eq("medicines_id",s).set("medicines_stock_num",medicinesStockNum.subtract(bigDecimal));
            medicinalDao.update(null,wrapper);
            System.out.println("66666666666666666666");
        }

    }
}



